using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace FrameWork
{
    public class TimeController : MonoBehaviour
    {
        [Header("时间设置")]
        [SerializeField] private float realWorldMinutesPerGameHour = 3f; // 现实世界3分钟等于游戏中的1小时
        [SerializeField] private int daysPerSeason = 30;                 // 每个季度的天数
        [SerializeField] private int quartersPerYear = 4;                // 每年的季度数

        [SerializeField] private TimeDataSO timeData;
        [SerializeField] private TimeView timeView;
        
        // 临时变量
        private int currentYear;
        private int currentSeason;
        private int currentDay;
        private int currentHour;
        private int currentDayOfWeek;
        
        //
        private Season season;

        protected void Awake()
        {
            timeData.AddListener(timeView.UpdateInfo);
        }

        private void Start()
        {
            timeData.Initialize();
            StartCoroutine(UpdateGameTime());
            
            // 保证Controller层临时变量 和 Model层数据一致
            currentDay = timeData.CurrentDay;
            currentYear = timeData.CurrentYear;
            currentSeason = timeData.CurrentSeason;
            currentDayOfWeek = timeData.CurrentDayOfWeek;
            currentHour = timeData.CurrentHour;
        }

        private void OnDisable()
        {
            timeData.RemoveListener(timeView.UpdateInfo);
        }

        private IEnumerator UpdateGameTime()
        {
            while (true)
            {
                // 每现实世界3分钟，游戏时间增加1小时
                yield return new WaitForSeconds(realWorldMinutesPerGameHour * 60);

                currentHour += 1;

                // 检查是否需要进位
                if (currentHour >= 24)
                {
                    currentHour = 0;
                    currentDay += 1;
                    currentDayOfWeek = (currentDayOfWeek + 1) % 7; // 更新星期几
                    
                    // 每天时间更新
                    EventSystem.Instance.EventTrigger(EventName.EVENT_TIME_UPDATE);

                    switch (currentSeason)
                    {
                        case 1: season = Season.春; break;
                        case 2: season = Season.夏; break;
                        case 3: season = Season.秋; break;
                        case 4: season = Season.冬; break;
                        default: season = Season.春; break;
                    }
                    EventSystem.Instance.EventTrigger(EventName.EVENT_TIME_WEATHER_UPDATE, season);

                    if (currentDay > daysPerSeason)
                    {
                        currentDay = 1;
                        currentSeason += 1;

                        if (currentSeason > quartersPerYear)
                        {
                            currentSeason = 1;
                            currentYear += 1;
                        }
                    }
                }
                
                // 更新数据
                timeData.UpdateTimeData(currentDay, currentYear, currentSeason, currentHour, currentDayOfWeek);
            }
        }
    }

}